package com.xyy.saas.payment.adpater.abchinaLoan.support;

import com.abchina.openbank.OpenBankHttpClient;
import com.xyy.saas.payment.adpater.abchinaLoan.config.AbchinaLoanConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Objects;

/**
 * @author wangzeqi
 */
@Slf4j
@Component
public class OpenBankHttpClientFactory {


    private final AbchinaLoanConfig abchinaLoanConfig;

    public OpenBankHttpClientFactory(AbchinaLoanConfig abchinaLoanConfig) {
        this.abchinaLoanConfig = abchinaLoanConfig;
    }


    @PostConstruct
    public void init() {
        try {
            String pfxFileName = abchinaLoanConfig.getPfxFileName();
            String cerFileName = abchinaLoanConfig.getCerFileName();
            InputStream pfxFileStream = new ClassPathResource(pfxFileName).getInputStream();
            InputStream cerFileStream = new ClassPathResource(cerFileName).getInputStream();
            OpenBankHttpClient.initOpenBankHttpClient(abchinaLoanConfig.getAppId(), pfxFileStream, abchinaLoanConfig.getPfxPwd(), cerFileStream, abchinaLoanConfig.getAppSecret());
            log.info("农行客户端初始化完成");
        } catch (Exception e) {
            log.error("Failed to initialize OpenBankHttpClient", e);
            throw new RuntimeException("农行客户端初始化异常");
        }

    }

}
